package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.mapper.MemberDAO;
import com.itheima.mapper.OrderDao;
import com.itheima.mapper.OrderSettingDao;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;

@Slf4j
@Service
public class OrderSercviceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDAO memberDAO;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;



    @Override
    @Transactional
    public void submitOrder(OrderDTO orderDTO) {
        //校验redis中的验证码是否正确
        String code = redisTemplate.opsForValue().get(orderDTO.getTelephone());
        if (code == null || !code.equals(orderDTO.getValidateCode())) {
            throw new RuntimeException("验证码错误");
        }
        //预约日期的预约人数没有设置的话不能预约。
        //预约日期是否已经约满，如果已经约满则无法预约。
        //不能重复预约（同一个用户在同一天预约了同一个套餐)
        //将date转为localdate
        LocalDate orderDate = orderDTO.getOrderDate().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting == null) {
            log.error("未找到指定日期的预约设置：{}", orderDate);
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }
        if (memberDAO.findByIdCard(orderDTO.getIdCard()) != null) {
            throw new RuntimeException(MessageConstant.HAS_ORDERED);
        }
        //预约成功后预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editNumberByOrderDate(orderDate,orderSetting.getReservations());
        //将orderDTO中的数据 分别封装到t_order 和 t_member中

        Member member = memberDAO.findByIdCard(orderDTO.getIdCard());
        if (member == null) {
            //不存在 封装一个新的对象
            //封装到member中
            //自动注册
            member = new Member();
            member.setName(orderDTO.getName());
            member.setSex(orderDTO.getSex());
            member.setPhoneNumber(orderDTO.getTelephone());
            member.setIdCard(orderDTO.getIdCard());
            //取现在时间Date封装进去
            member.setRegTime(new Date());
            memberDAO.add(member);
        }
        //如果存在，则无需添加
        //封装到order中
        Order order = new Order();
        order.setOrderDate(orderDTO.getOrderDate());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_TELEPHONE);
        order.setSetmealId(orderDTO.getSetmealId());
        //设置会员id
        order.setMemberId(member.getId());
        orderDao.add(order);
    }

    @Override
    public OrderVO findById(Integer id) {
        Order order = orderDao.findById(id);
        OrderVO orderVO = new OrderVO();
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        Member member = memberDAO.findById(order.getMemberId());
        if (member == null){
            throw new RuntimeException("会员不存在");
        }

        orderVO.setOrderDate(order.getOrderDate());
        orderVO.setOrderType(order.getOrderType());
        orderVO.setSetmeal(order.getSetmealId());
        orderVO.setMember(member.getName());

        return orderVO;
    }
}
